package net.zjitc.web;

import net.zjitc.Listener.WebApplicationContextUtils;
import net.zjitc.service.UserService;
import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取spring容器
        ServletContext servletContext = this.getServletContext();
        // 2.获取对象
//        ApplicationContext context = (ApplicationContext) servletContext.getAttribute("context");
//        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        // 3.调用方法
        UserService userService = context.getBean(UserService.class);
        // 4.执行方法
        userService.save();
    }
}




